local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "authsvrhelper"
require "enum"
local  AuthsvrRegister = {}

--[[
//请求注册
message RegisterReq {
	optional Version version = 1;   
	optional int32 auth_type = 2;	 // 账号类型 1 游客 2 微信 3 
	optional string user_name = 3;	 // 账号名
   	optional string cpass = 4; 		 // 注册时密码 client转换为utf8后md5值
    optional string regmobileid = 5; // 注册设备id
    optional string reglocation = 6; // 玩家注册位置
    optional string option_data = 7; // 可选数据
}

//响应client注册
message RegisterRes {
	optional int32 errcode = 1;    //错误原因 0表示成功
	optional string errcodedes = 2;//错误描述
	optional string user_name = 3; //账号名
	optional string cpass = 4;     //注册密码md5
	optional int32 uid = 5;        //账号uid
	optional int32 expiretime = 6; //过期时间戳
	optional string token = 7;  //登陆游戏服务器的认证token
}
]]
function  AuthsvrRegister.process(session, source, fd, request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}
	local server = msghelper.get_server()
	if request == nil 
		or request.version == nil 
		or request.regmobileid == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求！"
		msghelper:send_resmsgto_client(fd, "RegisterRes", responsemsg)
		server.tcpmng.close_socket(fd)
		return
	end

	if request.auth_type ~= EauthType.AUTH_TYPE_GUEST 
		and request.auth_type ~= EauthType.AUTH_TYPE_WECHAT
		and request.auth_type ~= EauthType.AUTH_TYPE_SELF then
		responsemsg.errcode = EErrCode.ERR_INVALID_AUTHTYPE
		responsemsg.errcodedes = "无效的账号类型！"
		msghelper:send_resmsgto_client(fd, "RegisterRes", responsemsg)
		server.tcpmng.close_socket(fd)
		return		
	end
	
	server.max_uid = server.max_uid + 1
	request.uid = server.max_uid
	server.tcpmng.create_session(fd, "RegisterReq", request)
end

return AuthsvrRegister
